package com.bw.gmall.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bw.gmall.bean.ProvinceBean;
import com.bw.gmall.domain.Pro;
import com.bw.gmall.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Random;


@RestController
@RequestMapping("/gmall/api")
public class ProductController {
    @Autowired
    private IProductService productService;
    @RequestMapping("/getGmv")
    public String getGmv(@RequestParam(value = "date", defaultValue = "0") Integer date) {
        BigDecimal gmv = productService.getGmv(date);
        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": "+(gmv.doubleValue()+new Random().nextInt(10000000))+"\n" +
                "}";
    }

    @RequestMapping("/getGmvByProvice")
    public String getGmvByProvice(@RequestParam(value = "date", defaultValue = "0") Integer date) {
        List<Pro> gmvByProvice = productService.getGmvByProvice(date);
        ProvinceBean provinceBean = new ProvinceBean();
        provinceBean.status = 0;
        provinceBean.msg = "";
        JSONObject jsonObject = new JSONObject();
        JSONArray mapData = new JSONArray();
        for (Pro p : gmvByProvice) {
            JSONObject object = new JSONObject();
            object.put("name",p.name);
            object.put("value",p.value);
            mapData.add(object);
        }
        jsonObject.put("mapData",mapData);
        jsonObject.put("valueName","销售额");
        provinceBean.data = jsonObject ;
        return JSONObject.toJSONString(provinceBean);
    }
}
